package cs.rsa.ts14.kilo;

import cs.rsa.ts14.framework.ClassType;
import cs.rsa.ts14.framework.ReportBuilder;
import cs.rsa.ts14.standard.ClassMap;

import java.util.Locale;

// TODO: Actual implementation. Move to actual source when completed.
public class WeekOverviewBuilder implements ReportBuilder {

    private Boolean begun = false;
    private String LineSeparator;
    private String Result;
    private WeekData thisWeek;
    private double HoursOvertime = 0;

    public WeekOverviewBuilder() {
        LineSeparator = System.getProperty("line.separator");
    }

    @Override
    public void buildBegin() {
        begun = true;
        Result = "=== Week Overview ===";
    }

    @Override
    public void buildWeekSpecification(int weekNo, int countWorkdays,
                                       int countUsedVacationdays) {
        if (!begun)
            return;
        // save this week
        if (thisWeek != null) {
            AddThisWeekToResult();
        }
        thisWeek = new WeekData();
        thisWeek.weekNumber = weekNo;
        thisWeek.workDays = countWorkdays;
        thisWeek.hoursWorked = 0.0;
    }

    @Override
    public void buildWorkSpecification(String category, String subCategory, double hours) {
        if (!begun)
            return;
        ClassType superClass = ClassMap.mapCategoryToClass(subCategory);
        if (superClass.equals(ClassType.CONSULENT))
            return; // Do not count this type of work
        thisWeek.hoursWorked += hours;
    }

    @Override
    public void buildWeekDaySpecification(String weekDay, String transportMode) {
        // this method isn't used for weekly overview, hence do nothing!
    }

    @Override
    public void buildAssignment(String variable, double value) {
        if (!begun)
            return;
        if (variable.equals("HoursOvertime")) {
            this.HoursOvertime = value;
        }
        // Otherwise do nothing as any other assigments isn't used by the weekly overview.
    }

    @Override
    public void buildEnd() {
        AddThisWeekToResult();
    }

    @Override
    public String getResult() {
        return Result;
    }

    private void AddThisWeekToResult() {
        double averageHoursPerDay = 0;
        if (thisWeek.workDays != 0)
            averageHoursPerDay = thisWeek.hoursWorked / thisWeek.workDays;
        this.HoursOvertime += (thisWeek.hoursWorked - 37);
        Result += LineSeparator;
        Result += String.format(Locale.ENGLISH, "Week %3d : %6.1f hours   ( %2d Wdays of %5.1f  d=%3.1f)",
                thisWeek.weekNumber, thisWeek.hoursWorked, thisWeek.workDays, averageHoursPerDay, this.HoursOvertime);
    }
}
